
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>2.2 定义一个模型 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.2">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="5.模型字段类型.html" />
    
    
    <link rel="prev" href="3.sequelize 实例变量.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="1.写在前面的话.html">
            
                <a href="1.写在前面的话.html">
            
                    
                    写在前面的话
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="2.搭建环境.html">
            
                <a href="2.搭建环境.html">
            
                    
                    第一章 搭建环境
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    第二章 模型定义
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="3.sequelize 实例变量.html">
            
                <a href="3.sequelize 实例变量.html">
            
                    
                    2.1 实例变量方法
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.4.2" data-path="4.定义一个模型.html">
            
                <a href="4.定义一个模型.html">
            
                    
                    2.2 定义一个模型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="5.模型字段类型.html">
            
                <a href="5.模型字段类型.html">
            
                    
                    2.3 模型字段类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="6.导入与配置.html">
            
                <a href="6.导入与配置.html">
            
                    
                    2.3 导入与配置
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="3模型关系.html">
            
                <a href="3模型关系.html">
            
                    
                    第三章 模型关系
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="3.1属于.html">
            
                <a href="3.1属于.html">
            
                    
                    3.1 属于
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="3.2包含.html">
            
                <a href="3.2包含.html">
            
                    
                    3.2 包含
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="3.3一对多.html">
            
                <a href="3.3一对多.html">
            
                    
                    3.3 一对多
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="3.4多对多.html">
            
                <a href="3.4多对多.html">
            
                    
                    3.4 多对多
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="3.5多态一对多.html">
            
                <a href="3.5多态一对多.html">
            
                    
                    3.5 多态一对多
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="3.6多态多对多.html">
            
                <a href="3.6多态多对多.html">
            
                    
                    3.6 多态多对多
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.7" data-path="3.7关系嵌套.html">
            
                <a href="3.7关系嵌套.html">
            
                    
                    3.7 关系嵌套
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="4模型查询.html">
            
                <a href="4模型查询.html">
            
                    
                    第四章 模型查询
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="5查询范围.html">
            
                <a href="5查询范围.html">
            
                    
                    第五章 查询范围 Scope (预定义查询条件)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="6生命周期函数.html">
            
                <a href="6生命周期函数.html">
            
                    
                    第六章 生命周期函数 Hook
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="7migrate.html">
            
                <a href="7migrate.html">
            
                    
                    第七章 数据库迁移 Migrate
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="8tabel2model.html">
            
                <a href="8tabel2model.html">
            
                    
                    第八章 根据 Table 生成 Model
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >2.2 定义一个模型</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x6A21;&#x578B;">&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x6A21;&#x578B;</h1>
<p>&#x6211;&#x4EEC;&#x8981;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x6A21;&#x578B;&#xFF0C;&#x9996;&#x5148;&#x6211;&#x4EEC;&#x4F1A;&#x7528;&#x5230;<code>define</code>&#xFF0C; &#x5B83;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x7C7B;&#x4F3C;&#x662F; <code>Model&lt;TInstance, TAttributes&gt;</code> &#x3002;</p>
<pre><code class="lang-ts">define&lt;TInstance, TAttributes&gt;(modelName: <span class="hljs-built_in">string</span>, attributes: DefineAttributes,
            options?: DefineOptions&lt;TInstance&gt;): Model&lt;TInstance, TAttributes&gt;;
</code></pre>
<p>&#x5728; defined &#x65B9;&#x6CD5;&#x4E2D;&#xFF0C; <code>&lt;TInstance, TAttributes&gt;</code> &#x8FD9;&#x4FE9;&#x6CDB;&#x578B;&#x4F1A;&#x4F20;&#x7ED9; <code>Model</code>&#x3002;&#x6CDB;&#x578B;&#x662F;&#x7528;&#x6765;&#x89E3;&#x51B3;&#x63CF;&#x8FF0;&#x67D0;&#x4E00;&#x7C7B;&#x578B;&#xFF0C;&#x4E14;&#x5728;&#x5B9A;&#x4E49;&#x4E4B;&#x524D;&#xFF0C;&#x65E0;&#x6CD5;&#x660E;&#x786E;&#x77E5;&#x9053;&#x67D0;&#x4E00;&#x53D8;&#x91CF;&#x7684;&#x7C7B;&#x578B;&#x7684;&#x95EE;&#x9898;&#x3002;&#x6362;&#x53E5;&#x8BDD;&#x8BF4;&#xFF0C;&#x5C31;&#x662F;&#x51FD;&#x6570;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#xFF0C;&#x8981;&#x4F9D;&#x8D56;&#x4F60;&#x4F20;&#x9012;&#x8FDB;&#x6765;&#x7684;&#x5BF9;&#x8C61;&#x7684;&#x7C7B;&#x578B;&#x3002;&#x51FD;&#x6570;&#x7684;&#x62EC;&#x53F7; <code>()</code> &#x4F20;&#x9012;&#x7684;&#x662F;&#x53C2;&#x6570;&#xFF0C;&#x6CDB;&#x578B;&#x7684; <code>&lt;&gt;</code> &#x5219;&#x662F;&#x4F20;&#x9012;&#x63CF;&#x8FF0;&#x7C7B;&#x578B;&#x3002;&#x63A5;&#x4E0B;&#x6765;&#x6211;&#x4EEC;&#x7EE7;&#x7EED;&#x770B; <code>Model</code> &#x7684;&#x5B9A;&#x4E49;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">interface</span> Model&lt;TInstance, TAttributes&gt; <span class="hljs-keyword">extends</span> Hooks&lt;TInstance&gt;, Associations {}
</code></pre>
<p>&#x63A5;&#x53E3;&#x662F;&#x652F;&#x6301;&#x7EE7;&#x627F;&#x7684;&#xFF0C;&#x63A5;&#x53E3;&#x662F;&#x63CF;&#x8FF0;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;&#x5C5E;&#x6027;&#x4E0E;&#x65B9;&#x6CD5;&#xFF0C;&#x5E76;&#x4E14;&#x8FDB;&#x884C;&#x7EA6;&#x675F;&#x3002;&#x6B64;&#x65F6; Model &#x5177;&#x6709; Hooks&#xFF0C;Associations &#x7684;&#x63CF;&#x8FF0;&#x3002;<code>Associations</code> &#x91CC;&#x9762;&#x5305;&#x542B;&#x5B9A;&#x4E49;&#x6A21;&#x578B;&#x76F4;&#x63A5;&#x5173;&#x7CFB;&#x7684;&#x4E00;&#x4E9B;&#x65B9;&#x6CD5;&#xFF0C;<code>Hooks</code> &#x662F;&#x5B9A;&#x4E49;&#x6A21;&#x578B;&#x751F;&#x547D;&#x5468;&#x671F;&#x7684;&#x4E00;&#x4E9B;&#x65B9;&#x6CD5;&#xFF08;&#x5220;&#x9664;&#x6570;&#x636E;&#x4E4B;&#x524D;&#xFF0C;&#x5220;&#x9664;&#x65B9;&#x6CD5;&#x4E4B;&#x540E;&#xFF0C;&#x521B;&#x5EFA;&#x65B9;&#x6CD5;&#x4E4B;&#x540E;....&#x7B49;&#x7B49;&#xFF09;&#x3002;&#x7136;&#x540E;&#x6211;&#x4EEC;&#x5728; <code>Model</code> &#x65B9;&#x6CD5;&#x91CC;&#x9762;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x770B;&#x5230;&#x5F88;&#x591A;&#x8FD4;&#x56DE;&#x503C;&#x4E3A;<code>TInstance</code> &#x7684;&#x7C7B;&#x578B;&#x7684;&#x51FD;&#x6570;&#x3002;</p>
<p><strong>&#x6BD4;&#x5982;</strong></p>
<pre><code class="lang-ts">build(record?: TAttributes, options?: BuildOptions): TInstance;
</code></pre>
<p><code>build</code> &#x662F;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x5B9E;&#x4F8B;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x5B83;&#x4F1A;&#x8FD4;&#x56DE; <code>TInstance</code> &#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#x8FD9;&#x4E2A; <code>TInstance</code> &#x8FD9;&#x4E2A;&#x7C7B;&#x578B; (interface) &#x8981;&#x6211;&#x4EEC;&#x81EA;&#x5DF1;&#x6765;&#x5199;&#x3002;</p>
<p><strong>1.&#x7528; interface &#x58F0;&#x660E;&#x4E00;&#x4E0B;&#x6A21;&#x578B;&#x6709;&#x54EA;&#x4E9B;&#x5C5E;&#x6027;&#xFF0C;&#x4E14;&#x662F;&#x4F55;&#x79CD;&#x7C7B;&#x578B;</strong></p>
<pre><code class="lang-ts"><span class="hljs-keyword">interface</span> UserAttributes {
  email: <span class="hljs-built_in">string</span>;
  name: <span class="hljs-built_in">string</span>;
}
</code></pre>
<p><strong>2.&#x58F0;&#x660E;&#x4E00;&#x4E2A;&#x63CF;&#x8FF0;&#x6A21;&#x578B;&#x7684;&#x5B9E;&#x4F8B;&#x53D8;&#x91CF;&#x7684;&#x7C7B;&#x578B;&#xFF0C;&#x8868;&#x660E;&#x53EF;&#x4EE5;&#x8BBF;&#x95EE;&#x7684;&#x5C5E;&#x6027;&#x548C;&#x65B9;&#x6CD5;</strong></p>
<pre><code class="lang-ts"><span class="hljs-keyword">interface</span> UserInstance <span class="hljs-keyword">extends</span> Sequelize.Instance&lt;UserAttributes&gt; {
  id: <span class="hljs-built_in">number</span>;
  createdAt: <span class="hljs-built_in">Date</span>;
  updatedAt: <span class="hljs-built_in">Date</span>;

  email: <span class="hljs-built_in">string</span>;
  name: <span class="hljs-built_in">string</span>;
}
</code></pre>
<blockquote>
<p><strong>FAQ: &#x4E3A;&#x4EC0;&#x4E48;&#x8981;&#x7EE7;&#x627F; <code>Sequelize.Instance&lt;UserAttributes&gt;</code> &#x5462;&#xFF1F;</strong><br><code>Sequelize.Instance</code> &#x91CC;&#x9762;&#x5B9A;&#x4E49;&#x4E86;&#x4E00;&#x4E9B;&#x6240;&#x6709;&#x6A21;&#x578B;&#x5B9E;&#x4F8B;&#x901A;&#x7528;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x800C;&#x4E14;&#x6211;&#x4EEC;&#x8FD8;&#x9700;&#x8981;&#x628A; <code>UserAttributes</code> (&#x6211;&#x4EEC;&#x81EA;&#x5B9A;&#x4E49;&#x7684;&#x5C5E;&#x6027;) &#x7ED9;&#x4F20;&#x9012;&#x8FDB;&#x53BB;&#x3002;  </p>
<p><strong>FAQ: &#x4F60;&#x4E3A;&#x4EC0;&#x4E48;&#x77E5;&#x9053;&#x9700;&#x8981;&#x7EE7;&#x627F; <code>Sequelize.Instance</code>&#xFF1F;</strong><br>&#x56E0;&#x4E3A;&#x6211;&#x53D1;&#x73B0;&#x4E0D;&#x7EE7;&#x627F;&#x7684;&#x8BDD;&#xFF0C;&#x901A;&#x8FC7;<code>build</code>&#x80FD;&#x8BBF;&#x95EE;&#x7684;&#x65B9;&#x6CD5;&#x91CC;&#x9762;&#x6839;&#x672C;&#x5C31;&#x6CA1;&#x6709;&#x5B98;&#x65B9; API &#x91CC;&#x9762;&#x6240;&#x8BF4;&#x7684;&#x90A3;&#x4E9B;&#x65B9;&#x6CD5;&#x3002;&#x8FD9;&#x91CC;&#x6211;&#x8BF4;&#x7684;&#x6CA1;&#x6709;&#x65B9;&#x6CD5;&#x662F;&#x6307; TypeScript &#x8FD9;&#x4E00;&#x4E2A;&#x5C42;&#x9762;&#x6CA1;&#x6709;&#xFF0C;&#x5047;&#x5982;&#x901A;&#x8FC7;&#xFF08;use as any&#xFF09;&#x53BB;&#x8C03;&#x7528;&#xFF0C;&#x8FD8;&#x662F;&#x53EF;&#x4EE5;&#x8C03;&#x7528;&#x5F97;&#x5230;&#x7684;&#xFF0C;&#x53EA;&#x662F; TypeScript &#x8FD9;&#x4E00;&#x5C42;&#x6CA1;&#x6709;&#x4EE3;&#x7801;&#x63D0;&#x793A;&#x4E86;&#x800C;&#x5DF2;&#x3002;&#x5F53;&#x6211;&#x9047;&#x5230;&#x8FD9;&#x4E2A;&#x95EE;&#x9898;&#x4E4B;&#x540E;&#xFF0C;&#x6211;&#x5C31;&#x60F3;&#x770B;&#x770B;&#x8001;&#x5916;&#x662F;&#x5982;&#x4F55;&#x5199;&#x7684;&#xFF0C;&#x6240;&#x4EE5;&#x6211;&#x5C31;&#x76F4;&#x63A5;&#x901A;&#x8FC7;&#x767E;&#x5EA6;<code>typescript sequelize</code> &#x5C31;&#x641C;&#x7D22;&#x5F97;&#x5230;&#x4E86;&#xFF0C;&#x4E4B;&#x540E;&#x6211;&#x53C8;&#x8FDB;&#x5165; d.ts &#x6587;&#x4EF6;&#x91CC;&#x9762;&#x53BB;&#x770B;&#x4E86;&#x4E00;&#x4E0B; <code>Instance</code> &#x8FD9;&#x4E2A;&#x63A5;&#x53E3;&#x3002;</p>
</blockquote>
<p>&#x8FD9;&#x91CC;&#x6211;&#x8FD8;&#x9047;&#x89C1;&#x4E86;&#x4E00;&#x4E2A;&#x95EE;&#x9898;&#x90A3;&#x5C31;&#x8FD9;&#x4E2A;&#x6A21;&#x578B;&#x6CA1;&#x6CD5;&#x4F7F;&#x7528; <code>new</code> &#x5173;&#x952E;&#x5B57;&#xFF0C;&#x56E0;&#x4E3A;&#x6CA1;&#x6709;<code>constructor</code>&#x6784;&#x9020;&#x5668;&#xFF0C;&#x4E5F;&#x6CA1;&#x6709; <code>prototype</code>&#xFF0C;&#x5F53;&#x7136;&#x8FD9;&#x91CC;&#x8BF4;&#x7684;&#x6CA1;&#x6709;&#xFF0C;&#x4E5F;&#x662F;&#x5728; <code>typescript</code> &#x8FD9;&#x4E2A;&#x5C42;&#x9762;&#x3002;&#x5BF9;&#x4E8E;&#x4E00;&#x5207;&#x62A5;&#x7C7B;&#x578B;&#x9519;&#x8BEF;&#x7684;&#x5927;&#x5BB6;&#x90FD;&#x53EF;&#x4EE5;&#x7528; <code>&#x53D8;&#x91CF; as any</code>&#xFF0C;&#x6765;&#x89E3;&#x51B3;&#x95EE;&#x9898;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x7C7B;&#x578B;&#x65AD;&#x8A00;&#xFF0C;&#x65AD;&#x8A00;&#x4E3A;&#x517C;&#x5BB9;&#x6240;&#x6709;&#x7C7B;&#x578B;&#x7684;&#x7C7B;&#x578B;&#x5373;&#x53EF;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x8BF4;&#x662F;&#x4E07;&#x80FD;&#x7C7B;&#x578B;&#x3002;  </p>
<p>&#x6211;&#x4EEC;&#x8FD8;&#x53EF;&#x4EE5;&#x6DF1;&#x5165;&#x770B;&#x4E00;&#x4E0B; <code>Instance</code> &#x63A5;&#x53E3;&#xFF0C;&#x5728; <code>2724 - 2922</code> &#x884C;&#xFF0C;&#x8FD9;&#x91CC;&#x9762;&#x90FD;&#x662F;&#x5B83;&#x53EF;&#x4EE5;&#x8BBF;&#x95EE;&#x7684;&#x65B9;&#x6CD5;&#x3002;</p>
<p><strong>3.&#x5B9A;&#x4E49;&#x6A21;&#x578B;</strong></p>
<pre><code class="lang-ts"><span class="hljs-keyword">const</span> S = Sequelize
<span class="hljs-keyword">const</span> User = sequelize.define&lt;UserInstance, UserAttributes&gt;
  (<span class="hljs-string">&apos;User&apos;</span>, {
    email: S.STRING,
    name: S.STRING
  });
</code></pre>
<p>&#x4E4B;&#x524D;&#x6211;&#x4EEC;&#x5B9A;&#x4E49;&#x7684;&#x5C5E;&#x6027;&#xFF0C;<code>id</code> &#x3001;<code>createdAt</code>&#x3001;<code>updateAt</code>&#x662F; sequelize &#x9ED8;&#x8BA4;&#x751F;&#x6210;&#x7684;&#xFF0C;&#x5F53;&#x7136;&#x4E5F;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x914D;&#x7F6E;&#x9879;&#x5173;&#x95ED;&#x81EA;&#x52A8;&#x65F6;&#x95F4;&#x6233;(&#x521B;&#x5EFA;&#x7684;&#x65F6;&#x95F4;&#xFF0C;&#x4FEE;&#x6539;&#x7684;&#x65F6;&#x95F4;)&#xFF0C;&#x6216;&#x8005;&#x81EA;&#x5DF1;&#x5B9A;&#x4E49; id &#x7684;&#x7C7B;&#x578B;&#x4E0E;&#x7EA6;&#x675F;&#xFF0C;sequelize &#x4F1A;&#x6709;&#x9650;&#x4EE5;&#x4F60;&#x7684;&#x914D;&#x7F6E;&#x4E3A;&#x6807;&#x51C6;&#x3002;&#x50CF;&#x4E0A;&#x9762;&#x6211;&#x4EEC;&#x5C31;&#x6CA1;&#x6709;&#x4F20;&#x9012; id &#x7684;&#x7C7B;&#x578B;&#x4E0E;&#x7EA6;&#x675F;&#xFF0C;sequelize &#x5C31;&#x7ED9;&#x6211;&#x4EEC;&#x81EA;&#x52A8;&#x8865;&#x4E0A;&#x4E86;&#xFF0C;&#x56E0;&#x4E3A;&#x4E00;&#x4E2A;&#x8868;&#x662F;&#x4E0D;&#x53EF;&#x80FD;&#x6CA1;&#x6709;&#x4E3B;&#x952E;&#x7684;&#x3002;</p>
<p><strong>4.&#x7ED9;&#x6A21;&#x578B;&#x5B9A;&#x4E49;&#x4E00;&#x4E9B;&#x65B9;&#x6CD5;</strong></p>
<pre><code class="lang-ts">(User as <span class="hljs-built_in">any</span>).prototype.say = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"><span class="hljs-keyword">this</span>: UserInstance</span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;name &apos;</span> + <span class="hljs-keyword">this</span>.name);
}
</code></pre>
<p><strong>Tip&#xFF1A;</strong>&#x6709;3&#x70B9;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x6CE8;&#x610F;&#x4E00;&#x4E0B;&#x3002;</p>
<ul>
<li>(User as any) &#x907F;&#x514D;&#x62A5;&#x9519;</li>
<li>&#x4F7F;&#x7528;&#x7684;&#x662F; function(){} &#x6765;&#x4FDD;&#x5B58; this &#x6307;&#x5411;&#xFF0C;&#x5047;&#x5982;&#x4F7F;&#x7528;&#x7BAD;&#x5934;&#x51FD;&#x6570;&#xFF0C;&#x5219; this &#x4E3A;&#x7A7A;&#x3002;</li>
<li>this: UserInstance &#x5728;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x7684;&#x4F4D;&#x7F6E;&#xFF0C;&#x53EF;&#x4EE5;&#x6307;&#x5B9A; this &#x7684;&#x7C7B;&#x578B;&#xFF0C;&#x8FD9;&#x6837; this &#x5C31;&#x6709;&#x7684;&#x4EE3;&#x7801;&#x63D0;&#x793A;&#x3002;</li>
</ul>
<p><strong>5.&#x8FD0;&#x884C;</strong></p>
<pre><code class="lang-ts">async <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">main</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">try</span> {
    await User.sync();
    <span class="hljs-keyword">const</span> user = User.build({ name: <span class="hljs-string">&apos;yugo&apos;</span>, email: <span class="hljs-string">&apos;soome@gmail.com&apos;</span> });
    await user.save();
    <span class="hljs-comment">// -----------------------</span>
    <span class="hljs-comment">// const user = await User.findAll();</span>
    <span class="hljs-comment">// (user[0] as any).say()</span>
    process.exit(<span class="hljs-number">0</span>)
  }<span class="hljs-keyword">catch</span>(e){
    <span class="hljs-built_in">console</span>.log(e)
  }
}

main()
</code></pre>
<p><code>await</code> &#x540E;&#x9762;&#x8DDF;&#x4E00;&#x4E2A; Promise &#x5BF9;&#x8C61;&#xFF0C;&#x50CF;<code>sync</code>&#x548C;<code>save</code>&#x8FD4;&#x56DE;&#x503C;&#x5176;&#x5B9E;&#x90FD;&#x662F;&#x4E00;&#x4E2A; Promise &#x5BF9;&#x8C61;&#x3002;<code>sync</code>&#x5C06;&#x6211;&#x4EEC; User &#x7684;&#x6A21;&#x578B;&#x540C;&#x6B65;&#x5230;&#x6570;&#x636E;&#x5E93;&#x8868;&#x683C;&#xFF0C;<code>build</code>&#x4F1A;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x6A21;&#x578B;&#x5B9E;&#x4F8B;&#xFF0C;&#x4F46;&#x5E76;&#x4E0D;&#x4F1A;&#x5B58;&#x50A8;&#xFF0C;&#x9700;&#x8981;&#x81EA;&#x884C;&#x8C03;&#x7528;<code>save</code>&#x65B9;&#x6CD5;&#xFF0C;&#x6216;&#x8005;&#x8C03;&#x7528;<code>create</code>&#x65B9;&#x6CD5;&#xFF0C;&#x5B83;&#x4F1A;&#x76F4;&#x63A5;&#x5B58;&#x50A8;&#x3002;&#x4E4B;&#x540E;&#x6211;&#x4EEC;&#x901A;&#x8FC7;<code>ts-node index.ts</code>&#x8FDB;&#x884C;&#x8FD0;&#x884C;&#xFF0C;&#x5F53;&#x6709;&#x6570;&#x636E;&#x4E86;&#x4E4B;&#x540E;&#xFF0C;&#x6211;&#x4EEC;&#x518D;&#x89E3;&#x5F00;&#x540E;&#x4FE9;&#x53E5;&#x7684;&#x6CE8;&#x91CA;&#xFF0C;&#x6CE8;&#x91CA;&#x524D;&#x4E09;&#x53E5;&#xFF0C;&#x518D;&#x6B21;&#x8FD0;&#x884C;&#x6D4B;&#x8BD5;&#x4E00;&#x4E0B;&#x67E5;&#x8BE2;&#x662F;&#x5426;&#x6B63;&#x5E38;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="3.sequelize 实例变量.html" class="navigation navigation-prev " aria-label="Previous page: 2.1 实例变量方法">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="5.模型字段类型.html" class="navigation navigation-next " aria-label="Next page: 2.3 模型字段类型">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"2.2 定义一个模型","level":"1.4.2","depth":2,"next":{"title":"2.3 模型字段类型","level":"1.4.3","depth":2,"path":"document/5.模型字段类型.md","ref":"./document/5.模型字段类型.md","articles":[]},"previous":{"title":"2.1 实例变量方法","level":"1.4.1","depth":2,"path":"document/3.sequelize 实例变量.md","ref":"./document/3.sequelize 实例变量.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"document/4.定义一个模型.md","mtime":"2017-06-19T05:39:34.000Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2017-06-28T08:29:37.466Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

